package cn.iocoder.yudao.module.erp.service.product;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductPurchaseInfoDO;
import cn.iocoder.yudao.module.erp.dal.mysql.product.ErpProductPurchaseInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PRODUCT_NOT_EXISTS;

/**
 * ERP 产品采购资料 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ErpProductPurchaseInfoServiceImpl implements ErpProductPurchaseInfoService {

    @Resource
    private ErpProductPurchaseInfoMapper productPurchaseInfoMapper;

    @Resource
    private ErpProductCategoryService productCategoryService;
    @Resource
    private ErpProductUnitService productUnitService;

    @Override
    public Long createProductPurchaseInfo(ProductPurchaseInfoSaveReqVO createReqVO) {
        // 插入
        ErpProductPurchaseInfoDO productPurchaseInfo = BeanUtils.toBean(createReqVO, ErpProductPurchaseInfoDO.class);
        productPurchaseInfoMapper.insertOrUpdate(productPurchaseInfo);
        // 返回
        return productPurchaseInfo.getFid();
    }

    @Override
    public void updateProductPurchaseInfo(ProductPurchaseInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateProductPurchaseInfoExists(updateReqVO.getFid());
        // 更新
        ErpProductPurchaseInfoDO updateObj = BeanUtils.toBean(updateReqVO, ErpProductPurchaseInfoDO.class);
        productPurchaseInfoMapper.insertOrUpdate(updateObj);
    }

    @Override
    public void deleteProductPurchaseInfo(Long fid) {
        // 校验存在
        validateProductPurchaseInfoExists(fid);
        // 删除
        productPurchaseInfoMapper.deleteById(fid);
    }

    @Override
    public List<ErpProductPurchaseInfoDO> validProductPurchaseInfoList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<ErpProductPurchaseInfoDO> list = productPurchaseInfoMapper.selectBatchIds(ids);
        Map<Long, ErpProductPurchaseInfoDO> productPurchaseInfoMap = convertMap(list, ErpProductPurchaseInfoDO::getFid);
        for (Long id : ids) {
            ErpProductPurchaseInfoDO productPurchaseInfo = productPurchaseInfoMap.get(id);
            if (productPurchaseInfoMap.get(id) == null) {
                throw exception(PRODUCT_NOT_EXISTS);
            }
        }
        return list;
    }

    private void validateProductPurchaseInfoExists(Long id) {
        if (productPurchaseInfoMapper.selectById(id) == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
    }

    @Override
    public ErpProductPurchaseInfoDO getProductPurchaseInfo(Long id) {
        return productPurchaseInfoMapper.selectOne(ErpProductPurchaseInfoDO::getFmaterialid, id);
    }

    @Override
    public List<ErpProductPurchaseInfoRespVO> getProductPurchaseInfoVOListByStatus(Integer status) {
        List<ErpProductPurchaseInfoDO> list = productPurchaseInfoMapper.selectListByStatus(status);
        return buildProductPurchaseInfoVOList(list);
    }

    @Override
    public List<ErpProductPurchaseInfoRespVO> getProductPurchaseInfoVOList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<ErpProductPurchaseInfoDO> list = productPurchaseInfoMapper.selectBatchIds(ids);
        return buildProductPurchaseInfoVOList(list);
    }

    private List<ErpProductPurchaseInfoRespVO> buildProductPurchaseInfoVOList(List<ErpProductPurchaseInfoDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        return BeanUtils.toBean(list, ErpProductPurchaseInfoRespVO.class, productPurchaseInfo -> {
        });
    }
}